//
//  WYCLoginViewController.m
//  renxinghuan
//
//  Created by 王晓民 on 2018/3/27.
//  Copyright © 2018年 吴清翠. All rights reserved.
//

#import "WYCLoginViewController.h"
#import "configuration.h"
#import "WQCResgistViewController.h"
#import "WQCForgetViewController.h"
#import "TabBarViewController.h"

@interface WYCLoginViewController ()<UITextFieldDelegate>
/** 账号*/
@property(nonatomic,strong)UITextField * userField;

/** 密码*/
@property(nonatomic,strong)UITextField * passwordField;
@end

@implementation WYCLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupCreat];
}

#pragma make ------->>创建界面
- (void)setupCreat{
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    //logo
    UIImageView * logoImageView = [[UIImageView alloc] init];
    logoImageView.frame = CGRectMake(ScreenWidth/2-96 , NavHeight+80, 193, 57);
    logoImageView.image = [UIImage imageNamed:@"logo"];
    [self.view addSubview:logoImageView];
    
    
    
    
    UIView * phoneView = [[UIView alloc] init];
    phoneView.frame = CGRectMake(40 , CGRectGetMaxY(logoImageView.frame) + 245 * px, ScreenWidth-80, 44);
    phoneView.backgroundColor = WYCRGBColor(249,249, 249);
    phoneView.layer.cornerRadius = 25;
    [self.view addSubview:phoneView];
    //手机号
    UIImageView * iphonelabel = [[UIImageView alloc] init];
    iphonelabel.frame = CGRectMake(21, 11, 21 , 21);
    iphonelabel.image = [UIImage imageNamed:@"icon_phone"];
    [phoneView addSubview:iphonelabel];
    
    UITextField * userTextField = [[UITextField alloc] init];
    userTextField.frame = CGRectMake(CGRectGetMaxX(iphonelabel.frame) + 30 * px,10, phoneView.width-60, 20);
    userTextField.backgroundColor = [UIColor clearColor];
    userTextField.placeholder = @"请输入手机号";
    userTextField.tintColor = wycMain;
    userTextField.delegate =self;
    userTextField.keyboardType = UIKeyboardTypeNumberPad;
    userTextField.clearButtonMode=UITextFieldViewModeAlways;
    [phoneView addSubview:userTextField];
    self.userField = userTextField;
    
    
    UIView * userView = [[UIView alloc] init];
    userView.frame = CGRectMake(40 , phoneView.y+69, ScreenWidth-80,44);
    userView.backgroundColor = WYCRGBColor(249,249, 249);
    userView.layer.cornerRadius = 25;
    [self.view addSubview:userView];
    
    
    
    //密码
    UIImageView * passlabel = [[UIImageView alloc] init];
    passlabel.frame = CGRectMake(21, 11, 21, 21);
    passlabel.image = [UIImage imageNamed:@"icon_password"];
    [userView addSubview:passlabel];
    
    
    UITextField * passTextField = [[UITextField alloc] init];
    passTextField.frame = CGRectMake(CGRectGetMaxX(iphonelabel.frame) + 30 * px,10, phoneView.width-60, 20);
    passTextField.backgroundColor = [UIColor clearColor];
    passTextField.placeholder = @"请输入密码";
    passTextField.tintColor = wycMain;
    passTextField.clearButtonMode=UITextFieldViewModeAlways;
    passTextField.secureTextEntry = YES;
    [userView addSubview:passTextField];
    self.passwordField = passTextField;
    
    
    //忘记密码？
    UIButton * forgetButton = [[UIButton alloc] init];
    forgetButton.frame = CGRectMake(ScreenWidth-150, userView.frame.size.height+userView.frame.origin.y+20, 100, 50 * px);
    forgetButton.titleLabel.font = [UIFont systemFontOfSize:WyzFont];
    [forgetButton setTitle:@"忘记密码？" forState:UIControlStateNormal];
    [forgetButton addTarget:self action:@selector(forgetButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [forgetButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [self.view addSubview:forgetButton];
    
    
    //登录按钮
    UIButton * loginButton = [[UIButton alloc] init];
    loginButton.frame = CGRectMake(userView.x, CGRectGetMaxY(userView.frame) + 70, userView.width, 150 * px);
    loginButton.backgroundColor = wycMain;
    loginButton.layer.cornerRadius = 25;
    [loginButton setTitle:@"登录" forState:UIControlStateNormal];
    loginButton.titleLabel.font = [UIFont systemFontOfSize:62 * px];
    [loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginButton addTarget:self action:@selector(loginButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginButton];
    UILabel  *regstLb = [MyUIClass makeUILabelWithFrame:CGRectMake(loginButton.x+30,  kScreenH-60, 120, 40*px) andBackColor:[UIColor clearColor] andText:@"还没有账号？点击" andTextColor:[UIColor lightGrayColor] andFont:[UIFont systemFontOfSize:14] andAlignment:NSTextAlignmentRight];
    [self.view addSubview:regstLb];
    
    //注册按钮
    UIButton * regsterButton = [[UIButton alloc] init];
    regsterButton.frame = CGRectMake(regstLb.frame.origin.x+regstLb.frame.size.width, kScreenH-60,100, 40 * px);
    [regsterButton setTitle:@"创建新账号" forState:UIControlStateNormal];
    regsterButton.titleLabel.textAlignment = NSTextAlignmentLeft;
    regsterButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [regsterButton setTitleColor:wycMain forState:UIControlStateNormal];
    [regsterButton addTarget:self action:@selector(rigesterClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:regsterButton];
    
    
    
    [self.userField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    
}

#pragma make -------->>快速注册
- (void)rigesterClick{
    WQCResgistViewController * rigester = [[WQCResgistViewController alloc] init];
   [self presentViewController:rigester animated:NO completion:nil];
}
-(void)textFieldDidChange:(UITextField *)textField
{
    CGFloat maxLength = 11;
    
    NSString *toBeString = textField.text;
    //获取高亮部分
    UITextRange *selectedRange = [textField markedTextRange];
    UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
    if (!position || !selectedRange)
    {
        if (toBeString.length > maxLength)
        {
            NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:maxLength];
            if (rangeIndex.length == 1)
            {
                textField.text = [toBeString substringToIndex:maxLength];
            }
            else
            {
                NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, maxLength)];
                textField.text = [toBeString substringWithRange:rangeRange];
            }
        }
    }
}
//-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
//{
//    if (textField == self.userField) {
//        if (textField.text.length > 10) {
//            return NO;
//        }
//    }
//    return YES;
//}
#pragma make --------->>忘记密码
- (void)forgetButtonClick{
   WQCForgetViewController * forget = [[WQCForgetViewController alloc] init];
   [self presentViewController:forget animated:NO completion:nil];
}

-(void)loginButtonClick
{
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]]; //字体颜色
    [SVProgressHUD setBackgroundColor:[UIColor blackColor]];   //背景颜色
    if (self.userField.text.length == 0){
        
       [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"手机号不能为空"];
        
        return;
    }
    
    if (self.passwordField.text.length == 0){
       [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"密码不能为空"];
       
        return;
    }
    
    
    //RSA密码加密
    NSString * passStr = [NSString stringWithFormat:@"%@",self.passwordField.text];
    NSString *pas = [RSA encryptString:passStr publicKey:RSA_P_KEY];
    
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    params[@"mobile"] = self.userField.text;
    
    //使用RSA加密操作的暗文
    params[@"password"] = pas;
    NSLog(@"%@",params);
    
    [HttpTool POST:Login params:params success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSLog(@"333%@",responseObject);
        if ([[responseObject objectForKey:@"code"] intValue] == 0){
            NSDictionary * dict = [responseObject objectForKey:@"result"];
            
            if(![[dict allKeys] containsObject:@"token"])
            {
                WYCAccount * Account = [[WYCAccount alloc] initWithDic:dict];

                [WYCAccountTool saveAccount:Account];

                [WYCIsAppLoginTool saveIsAppLogin:YES];
            
            }else{
                TabBarViewController *tabBarController = [[TabBarViewController alloc]init];
                [self  presentViewController:tabBarController animated:NO completion:nil];
                
                WYCAccount * Account = [[WYCAccount alloc] initWithDic:dict];
                
                [WYCAccountTool saveAccount:Account];
                
                [WYCIsAppLoginTool saveIsAppLogin:YES];
            }
            
        }else{
            
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:Failed];
    }];
   
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
